<?php

declare(strict_types=1);

namespace app\controller\admin;

use think\Request;
use think\facade\Validate;
use app\eslib\EsUtils;
use app\model\MigrationsModel;

/**
 * 数据迁移日志 控制器
 * @version 1.0.0
 */
class Migrations
{
    // 控制器中间件
    protected $middleware = [\app\middleware\JwtAuthForAdmin::class];

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $searchText = $request->param('search_text', '');
        $pageSize = intval($request->param('page_size', 10));

        $data = MigrationsModel::where(function ($query) use ($searchText) {
            if ($searchText) {
                $query->where(function ($query) use ($searchText) {
                    $query->where('id', intval($searchText))->whereOr('user_name', 'like', "%{$searchText}%");
                });
            }
        })->order('status')->order('id', 'desc')->append([])->paginate($pageSize);
        
        return json(EsUtils::stdMessage(true, '', $data));
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $formData = $request->param();
        $validator =  Validate::rule([
            'migration|用户名' => 'require|max:255',
'batch|' => 'require|number',
        ]);
        if (!$validator->check($formData)) {
            return json(EsUtils::stdMessage(false, $validator->getError()));
        }
        // 表单数据处理
        // ...
        try {
            $model = MigrationsModel::create($formData);
            return json(EsUtils::stdMessage(true, '', $model->id));
        } catch (\Exception $ex) {
            return json(EsUtils::stdMessage(false, $ex->getMessage()));
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        if (!is_numeric($id)) {
            return json(EsUtils::stdMessage(false, 'id值无效', $id));
        }
        if ($id > 0) {
            $model = MigrationsModel::find($id);
            if (!$model) {
                return json(EsUtils::stdMessage(false, '记录不存在'));
            }
        } else {
            $model = new MigrationsModel();
            $model->batch = 1;
        }
        $data = [
            'data' => $model,
            
        ];
        return json(EsUtils::stdMessage(true, $id, $data));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        // 特殊模式的更新操作
        $model = $request->param('_mode', '');
        switch ($model) {
            case 'copy':
                // 复制指定的资源
                try {
                    $affected = MigrationsModel::copy($id);
                    return json(EsUtils::stdMessage(true, '', $affected));
                } catch (\Exception $ex) {
                    return json(EsUtils::stdMessage(false, $ex->getMessage()));
                }
            case 'uoc':
                // uoc = update one column，更新某一字段
                $field = $request->param('k', '');
                $value = $request->param('v', '');
                try {
                    $affected = MigrationsModel::where('id', $id)->update([$field => $value]);
                    return json(EsUtils::stdMessage(true, '', $affected));
                } catch (\Exception $ex) {
                    return json(EsUtils::stdMessage(false, $ex->getMessage()));
                }
                break;
            default:
                break;
        }

        // 常规更新操作
        $formData = $request->param();
        if (isset($formData['_mode'])) {
            unset($formData['_mode']);
        }
        $validator =  \think\facade\Validate::rule([
            'migration|用户名' => 'require|max:255',
'batch|' => 'require|number',
        ]);
        if (!$validator->check($formData)) {
            return json(EsUtils::stdMessage(false, $validator->getError()));
        }
        try {
            $affected = MigrationsModel::where('id', $id)->update($formData);
            return json(EsUtils::stdMessage(true, '', $affected));
        } catch (\Exception $ex) {
            return json(EsUtils::stdMessage(false, $ex->getMessage()));
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        try {
            $affected = MigrationsModel::destroy($id);
            return json(EsUtils::stdMessage(true, '', $affected));
        } catch (\Exception $ex) {
            return json(EsUtils::stdMessage(false, $ex->getMessage()));
        }
    }
}
